package com.fare.taluo.application.impl.system.auth;

import java.util.ArrayList;
import java.util.List;

import org.openkoala.auth.application.vo.RoleVO;
import org.openkoala.koala.auth.core.domain.Role;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.system.auth.IdentityApplication;
import com.fare.taluo.base.ddd.application.BaseApplicationImpl;
import com.fare.taluo.util.DateUtils;

@Service("identityApplication")
@Transactional
public class IdentityApplicationImpl extends BaseApplicationImpl
	implements IdentityApplication {

	@Override
	public List<RoleVO> getUserRoles(String userAccount) {
		String hql = "select rua.role from RoleUserAuthorization rua where rua.user.userAccount=?";
		List<Role> roles = super.queryKoalaChannel().queryResult(hql, new Object[]{userAccount});
		List<RoleVO> vos = new ArrayList<RoleVO>();
		for(Role r : roles){
			vos.add(this.roleDomain2VO(r));
		}
		return vos;
	}
	
	private RoleVO roleDomain2VO(Role role){
		RoleVO vo = new RoleVO();
		vo.setAbolishDate(DateUtils.DATE_FMT_CN_BARS_YMD_HMS.format(role.getAbolishDate()));
		vo.setCreateDate(role.getCreateOwner());
		vo.setCreateOwner(role.getCreateOwner());
		vo.setId(role.getId());
		vo.setName(role.getName());
		vo.setRoleDesc(role.getRoleDesc());
		vo.setSerialNumber(role.getSerialNumber());
		vo.setSortOrder(role.getSortOrder());
		vo.setVersion(role.getVersion());
		return vo;
	}

}
